package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.JumpModel;
import com.app.service.AdminUserService;
import com.app.service.JumpService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.JumpVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 教育管理
 */
@RestController
@Controller
@RequestMapping("/api/jump")
public class JumpController {
    @Autowired
    private JumpService jumpService;
    @Autowired
    private AdminUserService adminUserService;


    //新增和编辑
    @PostMapping("/insertJump")
    public Result<Object> insertJump(@RequestBody JumpModel jumpModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (jumpModel.getId() > 0) {
            jumpModel.setUpdateTime(CommonHelp.getNowTimestamp());
            jumpService.update(jumpModel);
        } else {
            jumpModel.setCreateTime(CommonHelp.getNowTimestamp());
            jumpService.insert(jumpModel);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteJump")
    public Result<Object> deleteCar(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        jumpService.delete(id);
        return Result.success("操作成功");
    }


    @GetMapping("/detailJump")
    public Result<Object> detailJump(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(jumpService.getById(id));
    }

    //查询列表
    @GetMapping("/selectJump")
    public Result<Object> selectCar(Integer page, Integer limit, HttpServletRequest request) {
        // 校验token并获取用户信息
        AdminUserModel user = validateTokenAndGetUser(request);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Page<JumpModel> jumpModelPage = jumpService.selectCarList(page, limit);
        JumpVo vo = new JumpVo();
        vo.setList(jumpModelPage.getRecords());
        vo.setCount((int) jumpModelPage.getTotal());
        return Result.success(vo);
    }


    // 校验token并获取用户信息的方法
    private AdminUserModel validateTokenAndGetUser(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            throw new IllegalArgumentException("token不存在");
        }
        String userId = TokenUtils.verify(token);
        return adminUserService.getUserByUserId(Integer.parseInt(userId));
    }
}
